/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.crab.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kaaction.common.persistence.Page;
import com.kaaction.common.service.CrudService;
import com.kaaction.modules.crab.entity.CustomerManage;
import com.kaaction.modules.crab.entity.Order;
import com.kaaction.modules.crab.dao.CouponDao;
import com.kaaction.modules.crab.dao.CouponDetailDao;
import com.kaaction.modules.crab.dao.CrabAreaDao;
import com.kaaction.modules.crab.dao.CrabProductDao;
import com.kaaction.modules.crab.dao.CustomerManageDao;
import com.kaaction.modules.crab.dao.OrderDao;

/**
 * 订单管理Service
 * @author huanghaiming
 * @version 2017-07-21
 */
@Service
@Transactional(readOnly = true)
public class OrderService extends CrudService<OrderDao, Order> {
	
	@Autowired
	CouponDetailDao couponDetailDao;
	
	@Autowired
	CrabProductDao crabProductDao;
	
	@Autowired
	CrabAreaDao crabAreaDao;
	
	@Autowired
	CustomerManageDao customerManageDao;
	
	@Autowired
	CouponDao couponDao;

	public Order get(String id) {
		return super.get(id);
	}
	
	public List<Order> findList(Order order) {
		return super.findList(order);
	}
	
	public Page<Order> findPage(Page<Order> page, Order order) {
		String  numStart = order.getNumStart();
		String  numEnd = order.getNumEnd();
		if(StringUtils.isNotBlank(numStart)){
			if(numStart.length()>4){
				order.setNumStartOne(numStart.substring(0, numStart.length()-4));
				order.setNumOne(numStart.substring(numStart.length()-4, numStart.length()));
			}else{
				order.setNumStartOne("99887766");
			}
		}
		if(StringUtils.isNotBlank(numEnd)){
			if(numEnd.length()>4){
				order.setNumEndTwo(numEnd.substring(0, numEnd.length()-4));
				order.setNumTwo(numEnd.substring(numEnd.length()-4, numEnd.length()));
			}else{
				order.setNumEndTwo("99887766");
			}
		}
		return super.findPage(page, order);
	}
	
	@Transactional(readOnly = false)
	public void save(Order order) {
		
		if(StringUtils.isNotBlank(order.getId())){
			order.setState("1");
		}
		super.save(order);
		String id = order.getProductId();
		crabProductDao.updateNum(id);
		
	}
	
	@Transactional(readOnly = false)
	public void delete(Order order) {
		super.delete(order);
	}

	public List<Order> findOrder(Order order) {
		String  numStart = order.getNumStart();
		String  numEnd = order.getNumEnd();
		if(StringUtils.isNotBlank(numStart)){
			if(numStart.length()>4){
				order.setNumStartOne(numStart.substring(0, numStart.length()-4));
				order.setNumOne(numStart.substring(numStart.length()-4, numStart.length()));
			}else{
				order.setNumStartOne("99887766");
			}
		}
		if(StringUtils.isNotBlank(numEnd)){
			if(numEnd.length()>4){
				order.setNumEndTwo(numEnd.substring(0, numEnd.length()-4));
				order.setNumTwo(numEnd.substring(numEnd.length()-4, numEnd.length()));
			}else{
				order.setNumEndTwo("99887766");
			}
		}
		return super.findList(order);
	}

	@Transactional(readOnly = false)
	public void updateOrder(Order order) {
		order.preUpdate();
		order.setExpressCode("SF");
		super.dao.updateOrder(order);
		crabProductDao.updateNums(order.getOrderNum());
	}

	@Transactional(readOnly = false)
	public void insert(Order order) {
		Date date = new Date();
		order.preInsert();
		Random random = new Random();
		int rannum = (int) (random.nextDouble() * (99999 - 10000 + 1)) + 10000;// 获取5位随机数
		String orderNum = new SimpleDateFormat("yyyyMMdd").format(date) +rannum;
		order.setOrderNum(orderNum);
		order.setState("0");
		order.setCreateDate(date);
		String province = getArea(order.getProvince());
		String city = getArea(order.getCity());
		String county = getArea(order.getCounty());
		String address =province+city+county+order.getAddress();
		order.setAddress(address);
		order.setMsgState("0");
		super.dao.insert(order);
		
		
		CustomerManage customerManage = new CustomerManage();
		customerManage.preInsert();
		customerManage.setName(order.getName());
		customerManage.setMobile(order.getMobile());
		customerManage.setAddress(address);
		customerManage.setBuyDate(date);
		customerManage.setBuyProduct(crabProductDao.getProductName(order.getProductId()));
		customerManage.setNum(1);
		customerManage.setPrice(couponDao.getPrice(order.getCardNum(),order.getPassword()));
		customerManage.setRemarks(order.getRemarks());
		
		customerManageDao.insert(customerManage);
		couponDetailDao.updateState(order);
	}
	
	private String getArea(String str) {
		return crabAreaDao.selArea(str);
	}

	public Map<String, Object> selCoupon(String cardNum, String password) {
		return super.dao.selCoupon(cardNum,password);
	}

	public List<Order> selOrders() {
		return super.dao.selOrders();
	}

	@Transactional(readOnly = false)
	public int updateState(Order order) {
		return super.dao.updateState(order);
	}

	public int selEqual(String cardNum, String password) {
		return super.dao.selEqual(cardNum,password);
	}

	
}